/*******************************************************************************
 * Jimm - Mobile Messaging - J2ME ICQ clone
 * Copyright (C) 2003-04  Jimm Project
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 ********************************************************************************
 * File: src/jimm/comm/RequestInfoAction.java
 * Version: ###VERSION###  Date: ###DATE###
 * Author(s): Igor Palkin
 *******************************************************************************/


// #sijapp cond.if protocols_ICQ is "true" #
package org.jimm.protocol.icq.action;

import java.io.*;

import org.jimm.app.JimmException;
import org.jimm.comm.*;
import org.jimm.protocol.UserInfo;
import org.jimm.protocol.icq.packet.*;


public class SaveInfoAction extends IcqAction {
    // Receive timeout
    private static final int TIMEOUT = 5 * 1000; // milliseconds

    //TLVs
    private static final int NICK_TLV_ID = 0x0154;
    public static final int FIRSTNAME_TLV_ID = 0x0140;
    private static final int LASTNAME_TLV_ID = 0x014A;
    private static final int EMAIL_TLV_ID = 0x015E;
    private static final int BDAY_TLV_ID = 0x023A;
    private static final int GENDER_TLV_ID = 0x017C;
    private static final int HOME_PAGE_TLD = 0x0213;
    private static final int CITY_TLV_ID = 0x0190;
    private static final int STATE_TLV_ID = 0x019A;
    private static final int WORK_COMPANY_TLV = 0x01AE;
    private static final int WORK_DEPARTMENT_TLV = 0x01B8;
    private static final int WORK_POSITION_TLV = 0x01C2;

    /**
     * ************************************************************************
     */

    private UserInfo strData;

    // Date of init
    private long lastActivity;
    private int packetCounter;
    private int errorCounter;

    // Constructor
    public SaveInfoAction(UserInfo userInfo) {
        super();
        strData = userInfo;
    }

    // Init action
    public void init() throws JimmException {
        Util stream = new Util();

        /* 0x0C3A */
        stream.writeWordLE(ToIcqSrvPacket.CLI_SET_FULLINFO);

        stream.writeUtfzTLV(NICK_TLV_ID, strData.nick);
        stream.writeUtfzTLV(FIRSTNAME_TLV_ID, strData.firstName);
        stream.writeUtfzTLV(LASTNAME_TLV_ID, strData.lastName);

        stream.writeUtfzTLV(CITY_TLV_ID, strData.homeCity);
        stream.writeUtfzTLV(STATE_TLV_ID, strData.homeState);

        stream.writeUtfzTLV(HOME_PAGE_TLD, strData.homePage);
        stream.writeUtfzTLV(WORK_COMPANY_TLV, strData.workCompany);
        stream.writeUtfzTLV(WORK_DEPARTMENT_TLV, strData.workDepartment);
        stream.writeUtfzTLV(WORK_POSITION_TLV, strData.workPosition);

        /* Birsday */
        if (strData.birthDay != null) {
            String[] bDate = Util.explode(strData.birthDay, '.');
            try {
                if (bDate.length == 3) {
                    int year = Integer.parseInt(bDate[2]);
                    int month = Integer.parseInt(bDate[1]);
                    int day = Integer.parseInt(bDate[0]);
                    stream.writeWordLE(BDAY_TLV_ID);
                    stream.writeWordLE(6);
                    stream.writeWordLE(year);
                    stream.writeWordLE(month);
                    stream.writeWordLE(day);
                }
            } catch (Exception e) {
            }
        }

        /* Gender */
        stream.writeWordLE(GENDER_TLV_ID);
        stream.writeWordLE(1);
        stream.writeByte(strData.gender);

        /* Email */
        stream.writeTlvECombo(EMAIL_TLV_ID, strData.email, 0);

        /* Send packet */
        sendPacket(new ToIcqSrvPacket(0, getIcq().getUserId(), ToIcqSrvPacket.CLI_META_SUBCMD, new byte[0], stream.toByteArray()));

        /* Save date */
        lastActivity = System.currentTimeMillis();
    }


    // Forwards received packet, returns true if packet was consumed
    public boolean forward(Packet packet) throws JimmException {
        boolean consumed = false;

        // Watch out for SRV_FROMICQSRV packet
        if (packet instanceof FromIcqSrvPacket) {
            FromIcqSrvPacket fromIcqSrvPacket = (FromIcqSrvPacket) packet;

            // Watch out for SRV_META packet
            if (fromIcqSrvPacket.getSubcommand() != FromIcqSrvPacket.SRV_META_SUBCMD)
                return false;

            // Get packet data
            DataInputStream stream = Util.getDataInputStream(fromIcqSrvPacket.getData(), 0);

            try {
                int type = Util.getWordLE(stream);
                switch (type) {
                    case FromIcqSrvPacket.META_SET_FULLINFO_ACK: //  full user information
                    {
                        if (stream.readByte() != 0x0A) {
                            errorCounter++;
                            break;
                        }

                        consumed = true;
                        packetCounter++;
                        break;
                    }
                }
            } catch (Exception e) {
            }
        }

        return (consumed);
    }

    // Returns true if the action is completed
    public boolean isCompleted() {
        return (packetCounter >= 1);
    }

    // Returns true if an error has occured
    public boolean isError() {
        return (lastActivity + TIMEOUT < System.currentTimeMillis())
                || (errorCounter > 0);
    }
}
// #sijapp cond.end #
